import { _decorator, Component, instantiate, Vec3, Node, tween, Animation, Material, ModelComponent} from 'cc';
import Util from './Util';

const { ccclass } = _decorator;

@ccclass('SimpleCard') // 用 @ccclass 声明类为 Cocos 组件
export class SimpleCard extends Component {

    onLoad() {
        this.syncLayer();
    }

    syncLayer(): void {
        if(this.node.parent)
        {
            this.setLayerRecursively(this.node, this.node.parent.layer);
        }
    }

    setLayerRecursively(node: Node, layer: number): void {
        node.layer = layer;  // 设置当前节点的 layer
        // 遍历所有子节点并递归调用
        node.children.forEach(child => {
            this.setLayerRecursively(child, layer);
        });
    }
    
    setGray() {
        const model = this.node.getChildByName('Model');
        if(model)
        {
            // 替换材质
            Util.loadMaterial('Material/Card_Gray', (material) => {
                // 获取模型组件
                const modelComponent = model.getComponent(ModelComponent);
                if (modelComponent) {
                    // 替换模型的材质
                    modelComponent.material = material;
                } else {
                    console.error('ModelComponent not found on node:', model);
                }
            });
        }
    }

}